<?php

namespace App\Http\Controllers\Frontend;


use App\Logics\GoodsCateLogic;
use App\Logics\GoodsLogic;
use App\Logics\SiteConfigLogic;
use Illuminate\Support\Facades\Config;

class HomeController extends BaseController
{
    // 首页 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 列表

    // 页面初始化操作
    public function init()
    {
        // 系统配置
        $config = new SiteConfigLogic();
        $lists  = $config->config();
        if($lists === false){
            return $this->error($config->getError());
        }

        // 所有的栏目
        $cate = new GoodsCateLogic();
        $tree = $cate->tree();
        if($tree === false){
            return $this->error($cate->getError());
        }

        // 推荐的栏目
        $options = ['is_propose' => 1, 'is_page' => 0,];
        $cates   = $cate->lists($options);
        if($cates === false){
            return $this->error($cate->getError());
        }

        // 站点配置
        $site = Config::get('site');

        // 组合
        $res = ['propose' => $cates, 'tree' => $tree, 'user' => $this->myUser, 'site' => $site, 'config' => array_column($lists, 'value', 'name')];


        return $this->success($res);
    }

    // 首页商品
    public function goods()
    {
        // 8个 限时促销
        $options = ['is_get_cate_curr' => 0, 'is_get_cate_all_parent' => 0, 'is_get_cate_curr_cates' => 0, 'is_get_cate_top_cates' => 0, 'is_get_propose' => 0, 'is_get_no_cate' => 1, 'is_get_number' => 8, 'where_is_short_sale' => 1];
        $logic   = new GoodsLogic();
        $short   = $logic->lists($options);
        if($short === false){
            return $this->error($logic->getError());
        }

        // 8个 0.99
        $options = ['is_get_cate_curr' => 0, 'is_get_cate_all_parent' => 0, 'is_get_cate_curr_cates' => 0, 'is_get_cate_top_cates' => 0, 'is_get_propose' => 0, 'is_get_no_cate' => 1, 'is_get_number' => 8, 'where_is_start' => 1];
        $logic   = new GoodsLogic();
        $start99 = $logic->lists($options);
        if($start99 === false){
            return $this->error($logic->getError());
        }

        // 50个 推荐
        $ids     = array_unique(array_merge(array_column($short['list'], 'id'), array_column($start99['list'], 'id')));
        $options = ['is_get_cate_curr' => 0, 'is_get_cate_all_parent' => 0, 'is_get_cate_curr_cates' => 0, 'is_get_cate_top_cates' => 0, 'is_get_propose' => 1, 'is_get_no_cate' => 1, 'is_get_number' => 50, 'where_id_out' => $ids];
        $logic   = new GoodsLogic();
        $propose = $logic->lists($options);
        if($propose === false){
            return $this->error($logic->getError());
        }

        $result = ['propose' => $propose['list'], 'short' => $short['list'], 'start99' => $start99['list'],];

        return $this->success($result);
    }

}


